package com.basker.pisces.springcloud.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import com.basker.pisces.springcloud.locale.LocaleValidationMessageInterpolator;
import com.basker.pisces.springcloud.validation.ClientValidationInterceptor;

/**
 * 验证配置类,基于JSR303,springmvc默认使用hibernate的验证实现
 *
 * <p>
 * 添加自定义验证消息的拦截器，做多语言处理
 *
 * @author hangwen
 */
@Configuration
public class ValidationConfig {

    @Bean
    @ConditionalOnProperty(value = "pisces.feign.validate.enable", matchIfMissing = true)
    public ClientValidationInterceptor clientValidationInterceptor() {
        return new ClientValidationInterceptor();
    }

    @Bean
    public LocalValidatorFactoryBean defaultValidator() {
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        factoryBean.setMessageInterpolator(new LocaleValidationMessageInterpolator());

        return factoryBean;
    }

}
